<template>
  <view>
    <view
      class="search-bar"
      :class="[{ 'search-bar--fixed': fixed }]"
      :style="{ top: barTop, height: barHeight }"
    >
      <input
        type="text"
        v-model="inputVal"
        class="search-bar__input"
        :placeholder="placeholder"
        :maxlength="maxlength"
        @input="keywordInput"
        @change="keywordChange"
      />
      <view class="search-bar__btn am-flex-rc-center" @click="search"
        >搜索</view
      >
    </view>
    <view
      class="search-bar__place"
      :style="{ height: barHeight }"
      v-if="fixed"
    ></view>
  </view>
</template>

<script>
export default {
  name: 'co-search-bar',
  props: {
    barHeight: {
      type: String,
      default: () => '88rpx'
    },
    fixed: {
      type: Boolean,
      default: true
    },
    barTop: {
      type: String
    },
    value: {
      type: [String, Number]
    },
    placeholder: {
      type: String,
      default: '搜索'
    },
    maxlength: {
      type: Number,
      default: -1
    }
  },
  data() {
    return {
      inputVal: this.value
    }
  },
  watch: {
    value(n) {
      this.inputVal = n
    }
  },
  methods: {
    keywordInput() {
      this.$emit('input', this.inputVal)
    },
    keywordChange() {
      this.$emit('change', this.inputVal)
    },
    search() {
      this.$emit('search', this.inputVal)
    }
  }
}
</script>

<style lang="scss" scoped>
$page-plr: 16rpx;
.search-bar {
  display: flex;
  height: 88rpx;
  padding: 0 $page-plr;
  z-index: 400;
  align-items: center;
  background-color: #fff;
  box-shadow: 0px 1px 0px 0px #dddddd;
}
.search-bar--fixed {
  position: fixed;
  left: 0;
  width: 750rpx;
}
.search-bar__input {
  flex: 1;
  width: 0;
  height: 56rpx;
  padding: 0 25rpx;
  line-height: 56rpx;
  background: #efeff4;
  text-align: center;
  border-radius: 28rpx;
}
.search-bar__btn {
  height: 56rpx;
  padding: 0 18rpx;
  margin-left: 12rpx;
  flex-shrink: 0;
  font-size: 30rpx;
  font-weight: 400;
  color: #fe6507;
}
.search-bar__place {
}
</style>
